<?php
class ValidateCode
{
public static function initValidateCode($sessionName)
{
header("Content-type: image/PNG");
$im = imagecreate(44,18);
$back = ImageColorAllocate($im, 245,245,245);
imagefill($im,0,0,$back); //background
srand((double)microtime()*1000000);
//four digital
for($i=0;$i<4;$i++){
	$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
	$authnum=rand(1,9);
	$vcodes.=$authnum;
	imagestring($im,rand(2,5), 2+$i*10, 1, $authnum, $font);
}
for($i=0;$i<100;$i++) //interrupted dots
{ 
	$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
	imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
} 
ImagePNG($im);
ImageDestroy($im);
SessionUtil::setVar($sessionName,$vcodes);
}


public static function initUserSignin()
{
	ValidateCode::initValidateCode(constant('SESSION_KEY_USER_SIGN_VALIDATE_CODE'));
}
public static function isUserSigninCodeCorrect($code,$unset = true)
{	
	$valid = $code == SessionUtil::getVar(constant('SESSION_KEY_USER_SIGN_VALIDATE_CODE'));
	if($unset)
		SessionUtil::removeVar(constant('SESSION_KEY_USER_SIGN_VALIDATE_CODE'));
	return $valid;
}
}
?>